package edu.unindra.elearning.model;

import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class TableModelMateri extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 613065376123805069L;
	private Vector<Materi> row;
	private Vector<String> column;

	public TableModelMateri() {
		column = new Vector<String>();
		column.add("Tipe Materi");
		column.add("Kelas");
		column.add("Path Soal");
		column.add("Status");

		row = new Vector<Materi>();
	}

	public Materi getMateri(int index) {
		return row.get(index);
	}

	public void add(List<Materi> list) {
		for (Materi m : list) {
			row.add(m);
		}
		fireTableDataChanged();
	}

	public void removeAllElements() {
		row.removeAllElements();
		fireTableDataChanged();
	}

	/**
	 * 
	 * @param index
	 * @return
	 */
	public Materi removeMateri(int index) {
		Materi b = row.remove(index);
		fireTableRowsDeleted(index, index);
		return b;
	}

	public void setMateriAt(Materi obj, int index) {
		row.setElementAt(obj, index);
		fireTableRowsUpdated(index, index);
	}

	public boolean isEmpty() {
		return row.isEmpty();
	}

	public boolean contains(Object o) {
		return row.contains(o);
	}

	public void addMateri(Materi m) {
		row.add(m);
		fireTableDataChanged();
	}

	@Override
	public int getColumnCount() {
		return column.size();
	}

	@Override
	public int getRowCount() {
		return row.size();
	}

	@Override
	public String getColumnName(int column) {
		return this.column.get(column);
	}

	public void setValueAt(int rowIndex, int columnIndex, Object o) {
		try {
			if (columnIndex == 0) {
				row.get(rowIndex).setMateri(o.toString());
			} else if (columnIndex == 1) {
				row.get(rowIndex).setKelas(o.toString());
			} else if (columnIndex == 2) {
				row.get(rowIndex).setPath(o.toString());
			} else if (columnIndex == 3) {
				row.get(rowIndex).setStatus(o.toString());
			}
		} finally {
			fireTableDataChanged();
		}
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (columnIndex == 0) {
			return row.get(rowIndex).getMateri();
		} else if (columnIndex == 1) {
			return row.get(rowIndex).getKelas();
		} else if (columnIndex == 2) {
			return row.get(rowIndex).getPath();
		} else if (columnIndex == 3) {
			return row.get(rowIndex).getStatus();
		} else {
			return null;
		}
	}

}
